﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SqlDataAccess;
using IP.Core.IPCommon;

namespace QuanLyBanHang
{
    public partial class F800_Tra_cuu_danh_muc_san_pham : Form
    {
        public F800_Tra_cuu_danh_muc_san_pham()
        {
            InitializeComponent();
            set_define_events();
        }
        #region Members
        BKIQuanLyBanHangEntities db = new BKIQuanLyBanHangEntities();
        #endregion

        #region Private Methods

        private void set_define_events()
        {
            this.Load += F800_Tra_cuu_danh_muc_san_pham_Load;
            this.m_grv.Click += m_grv_Click;
            this.m_txt_tim_kiem_san_pham.TextChanged += m_txt_tim_kiem_san_pham_TextChanged;
        }

        private void load_data_to_grid()
        {
            var v_dm_san_pham = db.DM_SAN_PHAM
                                .Where(x => x.MA_SAN_PHAM.Contains(m_txt_tim_kiem_san_pham.Text.Trim())
                                || x.TEN_SAN_PHAM.Contains(m_txt_tim_kiem_san_pham.Text.Trim())
                                || m_txt_tim_kiem_san_pham.Text.Equals(""))
                                .OrderBy(x => x.MA_SAN_PHAM)
                                .Select(x => new
                                {
                                    ID = x.ID,
                                    MA_SAN_PHAM = x.MA_SAN_PHAM,
                                    TEN_SAN_PHAM = x.TEN_SAN_PHAM
                                })
                                .ToList();
            m_grv.DataSource = v_dm_san_pham;

            m_grv.Columns[0].HeaderText = "ID";
            m_grv.Columns[0].Visible = false;
            m_grv.Columns[1].HeaderText = "Mã sản phẩm";
            m_grv.Columns[1].Width = 100;
            m_grv.Columns[2].HeaderText = "Tên sản phẩm";
            m_grv.Columns[2].Width = 300;
            m_grv.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }
        #endregion
        private void F800_Tra_cuu_danh_muc_san_pham_Load(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_tim_kiem_san_pham_Click(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        void m_txt_tim_kiem_san_pham_TextChanged(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        void m_grv_Click(object sender, EventArgs e)
        {
            try
            {
                decimal v_dc_id_sp = (decimal)m_grv.CurrentRow.Cells[0].Value;
                var v_dm_san_pham = db.DM_SAN_PHAM.FirstOrDefault(x => x.ID == v_dc_id_sp);

                var v_dm_loai_san_pham = db.DM_LOAI_SAN_PHAM.FirstOrDefault(x => x.ID == v_dm_san_pham.ID_LOAI_SAN_PHAM);

                var v_dm_ncc = db.DM_NHA_CUNG_CAP.FirstOrDefault(x => x.ID == v_dm_san_pham.ID_NCC);

                var v_gd_so_du = db.GD_SO_DU.FirstOrDefault(x => x.ID_SAN_PHAM == v_dc_id_sp);

                var v_gd_don_gia = db.GD_DON_GIA.FirstOrDefault(x => x.ID_SAN_PHAM == v_dc_id_sp);
                if (v_dm_san_pham != null)
                {
                    m_txt_ma_san_pham.Text = v_dm_san_pham.MA_SAN_PHAM;
                    m_txt_ten_san_pham.Text = v_dm_san_pham.TEN_SAN_PHAM;
                    m_txt_ten_ncc.Text = v_dm_ncc.TEN_NCC;
                    m_txt_ten_loai_sp.Text = v_dm_loai_san_pham.TEN_LOAI_SAN_PHAM;
                    if (v_gd_so_du.SO_LUONG == 0)
                    {
                        m_txt_so_luong_con.Text = "0";
                    }
                    else
                    {
                        m_txt_so_luong_con.Text = CIPConvert.ToStr(v_gd_so_du.SO_LUONG);
                    }
                    m_txt_don_gia.Text = CIPConvert.ToStr(v_gd_don_gia.DON_GIA_BAN);

                    var v_lst_don_vi_tinh = db.DM_SAN_PHAM
                        .Where(x => x.ID == v_dc_id_sp)
                        .Select(x => new
                        {
                            DON_VI_TINH = x.DON_VI_TINH
                        })
                        .ToList();
                    m_cbo_don_vi_tinh.DisplayMember = "DON_VI_TINH";
                    m_cbo_don_vi_tinh.DataSource = v_lst_don_vi_tinh;
                }

            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }
        
        private void m_btn_back_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_grv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
